package com.wink_172.library.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * 2018/4/16.
 * 侧边栏 SideBar   联系人索引
 */

public class SideBar extends View {

    //触摸事件(用的接口回调)
    public void setCallback(ICallback callback) {
        this.mCallback = callback;
    }

    public interface ICallback {
        void onSendEvent(int event, String content);
    }


    public ICallback mCallback;

    //26个字母
    public static String[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "#"};


    private int choose = -1;// 选中
    private TextView mTextDialog;

    public void setTextView(TextView mTextDialog) {
        this.mTextDialog = mTextDialog;
    }

    public SideBar(Context context) {
        super(context);
    }

    public SideBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        int height = getHeight();//获取高度
        int width = getWidth();//获取宽度
        int singleHeight = height / b.length;//获取每一个字母的高度

        for (int i = 0; i < b.length; i++) {
            paint.setColor(Color.parseColor("#838383"));//未选中的颜色
            paint.setTextSize(30);
            paint.setAntiAlias(true);//抗锯齿功能
            // 选中的状态
            if (i == choose) {
                paint.setColor(Color.BLACK);//选中的颜色
                paint.setFakeBoldText(false); //true为粗体，false为非粗体            }
            }
            // x坐标等于中间-字符串宽度的一半.
            float xPos = width / 2 - paint.measureText(b[i]) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(b[i], xPos, yPos, paint);
            paint.reset();// 重置画笔
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();// 点击y坐标
        final int oldChoose = choose;

        final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
        switch (action) {
            case MotionEvent.ACTION_UP:
                setBackgroundDrawable(new ColorDrawable(0x00000000));
                choose = -1;//
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.INVISIBLE);
                }
                break;
            default:
                setBackgroundColor(Color.TRANSPARENT);
                if (oldChoose != c) {  //判断选中字母是否发生改变
                    if (c >= 0 && c < b.length) {
                        if (mCallback != null) {
                            mCallback.onSendEvent(0, b[c]);
                        }

                        if (mTextDialog != null) {
                            mTextDialog.setText(b[c]);
                            mTextDialog.setVisibility(View.VISIBLE);
                        }
                        choose = c;
                        invalidate();
                    }
                }
                break;
        }
        return true;
    }

    private static final String TAG = "SideBar";

}